package com.app.controller;

import com.app.common.BaseVo;
import com.app.common.enums.DataStatusEnum;
import com.app.bean.dto.RecommendQueryPage;
import com.app.entity.Recommend;
import com.app.service.IRecommendService;
import com.app.bean.vo.RecommendVO;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 推荐
 *
 * @autor xiaoyu.fang
 * @date 2019/10/10 13:36
 */
@RestController
@RequestMapping("/recommend")
public class RecommendController {

    @Autowired
    private IRecommendService IRecommendService;

    /**
     * 新增推荐
     *
     * @param recommend
     * @return
     */
    @PostMapping("/insert")
    public BaseVo insert(@Validated(Recommend.Create.class) @RequestBody Recommend recommend) {
        IRecommendService.insert(recommend);
        return new BaseVo();
    }

    /**
     * 获取推荐详细
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public BaseVo get(@RequestParam(value = "id") ObjectId id) {
        Recommend recommend = IRecommendService.findById(id);
        return new BaseVo(recommend);
    }

    /**
     * 获取推荐列表
     *
     * @return
     */
    @GetMapping("/query")
    public BaseVo query() {
        List<RecommendVO> list = IRecommendService.queryRecommendation();
        return new BaseVo(list);
    }

    /**
     * 查询分页
     *
     * @param queryPage
     * @return
     */
    @PostMapping("/queryPage")
    public BaseVo queryPage(@RequestBody RecommendQueryPage queryPage) {
        return new BaseVo(IRecommendService.queryPage(queryPage));
    }

    /**
     * 撤下推荐
     *
     * @param id
     * @return
     */
    @PostMapping("/repeal")
    public BaseVo repeal(@PathVariable(value = "id") ObjectId id) {
        IRecommendService.updateDataStatusById(DataStatusEnum.INVALID.KEY, id);
        return new BaseVo();
    }
}
